﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using AdvanceLib.Core;

namespace AdvanceLib.ScriptCore.Formatters
{
    public class HexadecimalFormatter : IFormatter
    {
        #region IFormatter Member
        public string Format(string input)
        {
            StringBuilder outputBuilder = new StringBuilder();

            LineParser lineParser = new LineParser();
            lineParser.Parse(input);

            foreach (Line line in lineParser.Lines)
            {
                if (!line.IsEmpty)
                {
                    outputBuilder.Append(line.Parts[0]);

                    for (int i = 1; i < line.Parts.Length; i++)
                    {
                        if (Regex.IsMatch(line.Parts[i], "^0x[0-9A-Fa-f]*$"))
                        {
                            line.Parts[i] = Convert.ToInt32(line.Parts[i], 16).ToString();
                        }

                        outputBuilder.Append(Constants.WHITE_SPACE);
                        outputBuilder.Append(line.Parts[i]);
                    }
                }
                outputBuilder.Append(Constants.NEW_LINE);
            }

            return outputBuilder.ToString();
        }
        #endregion
    }
}
